<template>
  <view class="content">
    <web-view :src="src" @message="hadnleMessage"></web-view>
  </view>
</template>

<script>
import base from "../../config/baseUrl";
export default {
  data() {
    return {
      isAddress: false,
      src: "https://app.diaochan88.com/map/#/",
    };
  },
  onLoad(e) {
    if (e.address) {
      this.isAddress = true;
    }
    // console.log('e', e);

    var pos = uni.getStorageSync("location");
    const safeArea = uni.getStorageSync("safeArea");
    const safeArea_top = safeArea.top;
	// console.log("pos", pos);
    if (pos) {
      // console.log('safeArea_top', safeArea_top);
      this.src = `${this.src}?lat=${pos.lat}&lng=${pos.lng}&top=${safeArea_top}`;
    }
  },
  methods: {
    hadnleMessage(e) {
      // webview ->>> uni 发送消息
      let data = e.detail.data[0];
      if (data) {
        if (data.type === "mapSelection") {
          // console.log("这是我收到的文本", data.location);
          if (this.isAddress) {
            this.getLocationInfoAddress(data.location);
          } else {
            this.getLocationInfo(data.location);
          }
        }
      }
    },
    getLocationInfo(location) {
      uni.setStorageSync("location", {
        lat: location.lat,
        lng: location.lng,
      });
      this.Commond.getAddressForPosMap(base.baseUrl, location.lat, location.lng)
        .then((address) => {
          // console.log('getLocationInfo', address);
          if (address) {
            const { ad_info } = address;
            const obj = {
              ...ad_info,
              address: address.address,
              lat: location.lat,
              lng: location.lng,
              location: {
                lat: location.lat,
                lng: location.lng,
              },
              latitude: location.lat,
              longitude: location.lng,
            };
            uni.setStorageSync("address_info", obj);
            uni.setStorageSync("map_tip", true);
          }
        })
        .finally(() => {
          uni.navigateBack();
        });
    },
    getLocationInfoAddress(location) {
      this.Commond.getAddressForPosMap(base.baseUrl, location.lat, location.lng)
        .then((address) => {
          if (address) {
            console.log("address", address);
            const { formatted_addresses, address_component } = address;
            const obj = {
              lat: location.lat,
              lng: location.lng,
              province: address_component.province,
              city: address_component.city,
              district: address_component.district,
              address: address.address,
              standard_address: formatted_addresses.standard_address,
            };
            uni.setStorageSync("user_address", obj);
            uni.setStorageSync("map_tip", true);
          }
        })
        .finally(() => {
          uni.navigateBack();
        });
    },
  },
};
</script>

<style></style>
